﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Partner.Utility
{
    public class QRCode
    {
        /// <summary>
        /// 编码AlphaNumeric，Numeric，Byte
        /// </summary>
        public QRCodeEncoder.ENCODE_MODE ENCODE_MODE { get; set; }
        /// <summary>
        /// 大小
        /// </summary>
        public int Size { get; set; }
        /// <summary>
        /// 版本（1-40）
        /// </summary>
        public int QRVersion { get; set; }
        /// <summary>
        /// 校正水平L，M，Q，H
        /// </summary>
        public QRCodeEncoder.ERROR_CORRECTION QRCodeErrorCorrect { get; set; }
        public string Data { get; set; }
        public Encoding Encoding { get; set; }
        public QRCode(string _data,
            QRCodeEncoder.ENCODE_MODE _ENCODE_MODE = QRCodeEncoder.ENCODE_MODE.BYTE,
            int _Size = 3,
            int _Version = 0,
            QRCodeEncoder.ERROR_CORRECTION _QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
            )
        {
            Data = _data;
            ENCODE_MODE = _ENCODE_MODE;
            Size = _Size;
            QRVersion = _Version;
            QRCodeErrorCorrect = _QRCodeErrorCorrect;
            Encoding = Encoding.UTF8;
        }
        /// <summary>
        /// 创建二维码图片（文本）
        /// </summary>
        /// <returns></returns>

        public Image CreateImage()
        {
            try
            {
                var encoder = new QRCodeEncoder();
                encoder.QRCodeEncodeMode = ENCODE_MODE;
                encoder.QRCodeScale = Size;
                encoder.QRCodeVersion = QRVersion;
                encoder.QRCodeErrorCorrect = QRCodeErrorCorrect;
                return encoder.Encode(Data);
            }
            catch { }
            return null;
        }
        /// <summary>
        /// 生成代图片的二维码
        /// </summary>
        /// <param name="imgCenter"></param>
        /// <returns></returns>
        public Image CreateImageAndImage(Image imgCenter)
        {
            Image img = CreateImage();
            return CombinImage(img, imgCenter);
        }
        #region 生成带图片的二维码

        /// <summary>
        ///     合并图片
        /// </summary>
        /// <param name="imgBack"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static Image CombinImage(Image imgBack, Image img)
        {
            if (img.Height != 50 || img.Width != 50)
            {
                if (img.Height == img.Width){
                    switch(img.Height){
                        case 30: img = ResizeImage(img, 30, 30, 0); break;
                        case 35: img = ResizeImage(img, 35, 35, 0); break;
                        case 40: img = ResizeImage(img, 40, 40, 0); break;
                        default: img = ResizeImage(img, 50, 50, 0); break;
                    }
                }
                else
                {
                    img = ResizeImage(img, 50, 50, 0);
                }
            }
            Graphics g = Graphics.FromImage(imgBack);

            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);   

            //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框  

            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);  

            g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
            GC.Collect();
            return imgBack;
        }

        /// <summary>
        ///     调用此函数后使此两种图片合并，类似相册，有个
        ///     背景图，中间贴自己的目标图片
        /// </summary>
        /// <param name="imgBack">粘贴的源图片</param>
        /// <param name="destImg">粘贴的目标图片</param>
        public static Image CombinImage(Image imgBack, string destImg)
        {
            Image img = Image.FromFile(destImg);
            return CombinImage(imgBack, img);
        }

        /// <summary>
        ///     Resize图片
        /// </summary>
        /// <param name="bmp">原始Bitmap</param>
        /// <param name="newW">新的宽度</param>
        /// <param name="newH">新的高度</param>
        /// <param name="mode">保留着，暂时未用</param>
        /// <returns>处理以后的图片</returns>
        public static Image ResizeImage(Image bmp, int newW, int newH, int mode)
        {
            try
            {
                Image b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                // 插值算法的质量  
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height),
                            GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch
            {
                return null;
            }
        }

        #endregion
    }

}
